package com.iflytek.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iflytek.common.Result;
import com.iflytek.dao.RoleDao;
import com.iflytek.document.RoleDocument;
import com.iflytek.document.UserDocument;
import com.iflytek.dto.PageDTO;
import com.iflytek.dto.RoleDTO;
import com.iflytek.mapper.RoleMapper;
import com.iflytek.model.Role;
import com.iflytek.model.User;
import com.iflytek.service.RoleService;
import com.iflytek.utils.CopyUtil;
import com.iflytek.vo.PageVO;
import com.iflytek.vo.RoleVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author h_t
 * @since 2023-09-23
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleDao roleDao;

    //查询所有角色
    @Override
    public Result<List<RoleVO>> findAll(RoleDTO dto) {
        List<Role> roleList = new ArrayList<>();
        Iterable<RoleDocument> all = roleDao.findAll();
        List<RoleDocument> roleDocuments = new ArrayList<>();
        all.forEach(s->{
            roleDocuments.add(s);
        });
        if (roleDocuments.isEmpty()){
            roleList = CopyUtil.copyList(roleDocuments, Role.class);
        }else {
            QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
            roleList = roleMapper.selectList(queryWrapper);
        }
        List<RoleVO> roleVOS = CopyUtil.copyList(roleList, RoleVO.class);


        return Result.success(roleVOS);
    }

    //角色分页查询
    @Override
    public Result<PageVO<RoleVO>> getPage(PageDTO<RoleDTO> dto) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(dto.getName())) {
            queryWrapper.lambda().like(Role::getName, dto.getName());
        }
        if (dto.getPageNum() == null) {
            dto.setPageNum(1);
        }
        if (dto.getSize() == null) {
            dto.setSize(5);
        }
        PageHelper.startPage(dto.getPageNum(), dto.getSize());

        List<Role> roleList = roleMapper.selectList(queryWrapper);
        List<RoleVO> permissionVOList = CopyUtil.copyList(roleList, RoleVO.class);

        PageInfo<Role> rolePageInfo = new PageInfo<>();
        List<RoleVO> roleVOS = CopyUtil.copyList(roleList, RoleVO.class);
        PageVO<RoleVO> pageVO = new PageVO<>();
        BeanUtils.copyProperties(dto, pageVO);
        pageVO.setTotal(rolePageInfo.getTotal());
        pageVO.setRecords(roleVOS);
        return Result.success(pageVO);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveRe(RoleDTO dto) {
        Role role = new Role();
        BeanUtils.copyProperties(dto, role);

        int count = roleMapper.insert(role);

        if (count > 0) {

            RoleDocument copy = CopyUtil.copy(dto, RoleDocument.class);
            roleDao.save(copy);
            return Result.success(true);
        } else {
            return Result.error("1", "新增失败");
        }

    }

    //通过id查询角色信息
    @Override
    public Result<RoleVO> findById(Long id) {
        Role role;
        Optional<RoleDocument> byId = roleDao.findById(id);
        RoleDocument document = byId.get();
        if (document != null){
            role = CopyUtil.copy(document, Role.class);
        }else
        {
            role = roleMapper.selectById(id);
        }
        RoleVO roleVO = CopyUtil.copy(role, RoleVO.class);
        return Result.success(roleVO);
    }

    @Override
    public Result<Boolean> updateRole(RoleDTO dto) {
        if (dto == null) {
            return Result.error("2", "数据不能为空");
        }
        Role role = CopyUtil.copy(dto, Role.class);

        roleMapper.selectById(dto.getId());
        role.setPermission(dto.getPermission().toString());
        int count = roleMapper.updateById(role);
        if (count == 0) {
            return Result.error("1", "更新失败");
        }
        roleDao.deleteById(dto.getId());
        RoleDocument copy = CopyUtil.copy(role, RoleDocument.class);
        copy.setPermission(dto.getPermission());
        roleDao.save(copy);

        return Result.success(true);
    }


    //实现通过角色id删除角色
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> DeleteRole(Long id) {
        // 在数据库中查找用户
        int role = roleMapper.deleteById(id);

        if (role > 0) {
            roleDao.deleteById(id);
            // 如果成功，返回成功信息
            return Result.success(true);
        }
        // 如果用户不存在，返回错误信息
        return Result.error("0", "删除失败");
    }




}